package com.intelligent.mts.client.core.modal;


import com.google.common.collect.Lists;
import com.intelligent.mts.client.core.controller.MainController;
import com.intelligent.mts.client.utils.ApplicationBeanUtils;
import com.intelligent.mts.client.utils.SystemConfig;
import com.intelligent.mts.common.utils.StringUtils;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * Created by dmsoft on 2016/10/14.
 */
public class TestStationModal extends AbstractTableModel{

    private String columns[];

    private List<String> data;

    private MainController mainController;

    public TestStationModal(String columns[]){
        this.columns = columns;
    }


    @Override
    public int getRowCount() {
        if(data == null){
            return 0;
        }else {
            return data.size();
        }
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }
    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        setService();
        String stationId = data.get(rowIndex);
        if (columnIndex == 0) {
            return stationId;
        } else if (columnIndex == 1) {
            return mainController.switchProductIdByStationId(stationId);
        } else if (columnIndex == 2) {
            return mainController.findFgpnByStationId(stationId);
        } else if (columnIndex == 3) {
            return mainController.findSerialNumberByStationId(stationId);
        }
        return null;
    }
    public void setData() {
        SystemConfig systemConfig = ApplicationBeanUtils.getBean(SystemConfig.class);
        if (StringUtils.isBlank(systemConfig.getTestStations())) {
            return;
        }
        List<String> StationIdLists = Lists.newArrayList();
        String[] stationIds = systemConfig.getTestStations().split(",");
        for (String stationId : stationIds) {
            StationIdLists.add(stationId);
        }
        this.data = StationIdLists;
        fireTableDataChanged();
    }

    public void setService() {
        if (mainController == null) {
            mainController = ApplicationBeanUtils.getBean(MainController.class);
        }
    }
}
